﻿@inject IDataCacheService _dataCacheService
@inject IMauiService _mauiService
@inject ILocalStorageService _localStorage
@inject IEventService _eventService
@inject IJSRuntime JS
@inject NavigationManager NavigationManager
@inject MasaBlazor MasaBlazor
@implements IDisposable

<!--主题颜色-->
<link rel="stylesheet" href="@($"https://app.cngal.org/_content/CnGalWebSite.Components/css/material-theme/color-tokens/color-{_themeColor??_dataCacheService.ThemeSetting.ThemeColor}-tokens.css")">


<!--主题模式-->
@if (_dataCacheService.ThemeSetting.ThemeMode == ThemeMode.System && _isDark != true)
{
    <!--跟随系统-->
    @* <link rel="stylesheet" href="_content/CnGalWebSite.Shared/css/theme.css" /> *@
    <link rel="stylesheet" href="https://app.cngal.org/_content/CnGalWebSite.Shared/css/theme.css" />
}
else if (_dataCacheService.ThemeSetting.ThemeMode == ThemeMode.Dark || _isDark == true)
{
    <!--暗色模式-->
    <link rel="stylesheet" href="https://app.cngal.org/_content/CnGalWebSite.Shared/css/dark/bundle-dark.min.css" />
    @* <link rel="stylesheet" href="_content/CnGalWebSite.Shared/css/dark/bundle-dark.css" /> *@
    <link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/10.6.0/styles/vs2015.min.css" />
}
else if (_dataCacheService.ThemeSetting.ThemeMode == ThemeMode.Light)
{
    <!--亮色模式-->
    <link rel="stylesheet" href="https://app.cngal.org/_content/CnGalWebSite.Shared/css/light/bundle-light.min.css" />
    @* <link rel="stylesheet" href="_content/CnGalWebSite.Shared/css/light/bundle-light.css" /> *@
    <link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/10.6.0/styles/atom-one-light.min.css" />
}

<!--字体-->
@if (string.IsNullOrWhiteSpace(_dataCacheService.ThemeSetting.FontFamily) == false)
{
    <style>
        body {
            font-family: @_dataCacheService.ThemeSetting.FontFamily !important;
        }
    </style>
}

<!--窄边距-->
@if (_dataCacheService.ThemeSetting.IsExtendEntireScreen)
{
    <link rel="stylesheet" href="https://app.cngal.org/_content/CnGalWebSite.Shared/css/share/extend-entire-screen.css">
    @* <link rel="stylesheet" href="_content/CnGalWebSite.Shared/css/share/extend-entire-screen.css"> *@
}

<!--无边距-->
@if (_isFullScreen ?? _dataCacheService.ThemeSetting.IsFullScreen)
{
    <style>
        .variable.container {
            max-width: none;
            padding: 0;
        }
    </style>
}

@code {
    [Parameter]
    public bool? Mobile { get; set; }

    ThemeModel _themeSetting = new ThemeModel();

    bool? _isDark;
    bool? _isTransparent;
    bool? _isFullScreen;
    string _themeColor;


    protected override void OnInitialized()
    {
        _eventService.SavaTheme -= OnSavaTheme;
        _eventService.SavaTheme += OnSavaTheme;

        _eventService.CleanTempEffectTheme -= OnCleanTempEffectTheme;
        _eventService.CleanTempEffectTheme += OnCleanTempEffectTheme;

        _eventService.TempEffectTheme -= OnTempEffectTheme;
        _eventService.TempEffectTheme += OnTempEffectTheme;
    }

    public async void OnTempEffectTheme(bool? isDark, bool? isFullScreen, bool? isTransparent, string themeColor)
    {
        _isDark = isDark;
        _isTransparent = isTransparent;
        _isFullScreen = isFullScreen;
        _themeColor = themeColor;
        await Refresh();
    }

    public async void OnSavaTheme()
    {
        await SaveTheme();
        await Refresh();
    }
    public async void OnCleanTempEffectTheme()
    {
        _isTransparent = null;
        _themeColor = null;
        _isFullScreen = null;
        _isDark = null;
        await Refresh();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            //读取本地主题配置
            await LoadTheme();
            //刷新
            await Refresh();
        }
    }

    /// <summary>
    /// 读取本地主题配置
    /// </summary>
    /// <returns></returns>
    public async Task LoadTheme()
    {
        _dataCacheService.ThemeSetting = await _localStorage.GetItemAsync<ThemeModel>("theme");
        await SaveTheme();
    }

    /// <summary>
    /// 检查主题设置
    /// </summary>
    public async Task CheckTheme()
    {
        if (_dataCacheService.ThemeSetting == null)
        {
            _dataCacheService.ThemeSetting = new ThemeModel();
        }
        if (ThemeModel.AvailableColors.Contains(_dataCacheService.ThemeSetting.ThemeColor) == false)
        {
            _dataCacheService.ThemeSetting.ThemeColor = ThemeModel.DefaultThemeColor;
        }
        //获取系统主题
        if (_dataCacheService.ThemeSetting.ThemeMode == ThemeMode.System)
        {
            _dataCacheService.ThemeSetting.IsDark = await JS.InvokeAsync<bool>("checkSystemThemeIsDark");
        }
        else
        {
            _dataCacheService.ThemeSetting.IsDark = _dataCacheService.ThemeSetting.ThemeMode == ThemeMode.Dark;
        }
    }

    /// <summary>
    /// 保存本地主题配置
    /// </summary>
    /// <returns></returns>
    public async Task SaveTheme()
    {
        await CheckTheme();
        await _localStorage.SetItemAsync("theme", _dataCacheService.ThemeSetting);
    }

    /// <summary>
    /// 刷新
    /// </summary>
    public async Task Refresh()
    {
        await InvokeAsync(StateHasChanged);
        _eventService.OnKanbanChanged();
    }

    #region 释放实例
    public void Dispose()
    {
        _eventService.SavaTheme -= OnSavaTheme;
        _eventService.TempEffectTheme -= OnTempEffectTheme;
        _eventService.CleanTempEffectTheme -= OnCleanTempEffectTheme;
        GC.SuppressFinalize(this);
    }
    #endregion
}
